home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu248.dms / pu248.adf / Intuition / Menus / Example1.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  15KB  |  389 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Menus                       Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program opens a normal window to which we connect a menu strip. */
  21. /* The menu will look like this:                                        */
  22. /*                                                                      */
  23. /* Mode                                                                 */
  24. /* -----------------                                                    */
  25. /* | v Plain       |                                                    */
  26. /* |   Bold        |                                                    */
  27. /* |   Underlined  |                                                    */
  28. /* |   Italic      |                                                    */
  29. /* -----------------                                                    */
  30. /*                                                                      */
  31. /* The user can select either Plain or a combination of the other       */
  32. /* styles. (If the user selects Plain all other modes will be mutual    */
  33. /* excluded, but if the user on the other hand selects Bold, Underlined */
  34. /* or Italic, the Plain option will be mutul excluded.                  */
  35. /*                                                                      */
  36. /* This example also shows how a program should handle the IDCMP flags, */
  37. /* and how to collect several messages from one single menu event.      */
  38.  
  39.  
  40.  
  41. #include <intuition/intuition.h>
  42.  
  43.  
  44.  
  45. struct IntuitionBase *IntuitionBase;
  46.  
  47.  
  48.  
  49. /*************************************************************************/
  50. /*                         F O U R T H   I T E M                         */
  51. /*************************************************************************/
  52.  
  53. /* The text for the fourth item: */
  54. struct IntuiText my_fourth_text=
  55. {
  56.   2,            /* FrontPen, black. */
  57.   0,            /* BackPen, not used since JAM1. */
  58.   JAM1,         /* DrawMode, do not change the background. */
  59.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  60.                 /* This will leave enough space for the check mark. */
  61.   1,            /* TopEdge, 1 line down. */
  62.   NULL,         /* TextAttr, default font. */
  63.   "Italic",     /* IText, the string. */
  64.   NULL          /* NextItem, no link to other IntuiText structures. */
  65. };
  66.  
  67. /* The MenuItem structure for the fourth item: */
  68. struct MenuItem my_fourth_item=
  69. {
  70.   NULL,            /* NextItem, this is the last item in the list. */
  71.   0,               /* LeftEdge, 0 pixels out. */
  72.   30,              /* TopEdge, 30 lines down. */
  73.   150,             /* Width, 150 pixels wide. */
  74.   10,              /* Height, 10 lines high. */
  75.   ITEMTEXT|        /* Flags, render this item with text. */
  76.   ITEMENABLED|     /*        this item will be enabled. */
  77.   CHECKIT|         /*        it is an attribute item. */
  78.   HIGHCOMP,        /*        complement the colours when highlihted. */
  79.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  80.   (APTR) &my_fourth_text, /* ItemFill, pointer to the text. */
  81.   NULL,            /* SelectFill, nothing since we complement the col. */
  82.   0,               /* Command, no command-key sequence. */
  83.   NULL,            /* SubItem, no subitem list. */
  84.   MENUNULL,        /* NextSelect, no items selected. */
  85. };
  86.  
  87.  
  88.  
  89. /*************************************************************************/
  90. /*                          T H I R D   I T E M                          */
  91. /*************************************************************************/
  92.  
  93. /* The text for the third item: */
  94. struct IntuiText my_third_text=
  95. {
  96.   2,            /* FrontPen, black. */
  97.   0,            /* BackPen, not used since JAM1. */
  98.   JAM1,         /* DrawMode, do not change the background. */
  99.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  100.                 /* This will leave enough space for the check mark. */
  101.   1,            /* TopEdge, 1 line down. */
  102.   NULL,         /* TextAttr, default font. */
  103.   "Underlined", /* IText, the string. */
  104.   NULL          /* NextItem, no link to other IntuiText structures. */
  105. };
  106.  
  107. /* The MenuItem structure for the third item: */
  108. struct MenuItem my_third_item=
  109. {
  110.   &my_fourth_item, /* NextItem, linked to the fourth item. */
  111.   0,               /* LeftEdge, 0 pixels out. */
  112.   20,              /* TopEdge, 20 lines down. */
  113.   150,             /* Width, 150 pixels wide. */
  114.   10,              /* Height, 10 lines high. */
  115.   ITEMTEXT|        /* Flags, render this item with text. */
  116.   ITEMENABLED|     /*        this item will be enabled. */
  117.   CHECKIT|         /*        it is an attribute item. */
  118.   HIGHCOMP,        /*        complement the colours when highlihted. */
  119.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  120.   (APTR) &my_third_text, /* ItemFill, pointer to the text. */
  121.   NULL,            /* SelectFill, nothing since we complement the col. */
  122.   0,               /* Command, no command-key sequence. */
  123.   NULL,            /* SubItem, no subitem list. */
  124.   MENUNULL,        /* NextSelect, no items selected. */
  125. };
  126.  
  127.  
  128.  
  129. /*************************************************************************/
  130. /*                         S E C O N D   I T E M                         */
  131. /*************************************************************************/
  132.  
  133. /* The text for the second item: */
  134. struct IntuiText my_second_text=
  135. {
  136.   2,          /* FrontPen, black. */
  137.   0,          /* BackPen, not used since JAM1. */
  138.   JAM1,       /* DrawMode, do not change the background. */
  139.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  140.               /* This will leave enough space for the check mark. */
  141.   1,          /* TopEdge, 1 line down. */
  142.   NULL,       /* TextAttr, default font. */
  143.   "Bold",     /* IText, the string. */
  144.   NULL        /* NextItem, no link to other IntuiText structures. */
  145. };
  146.  
  147. /* The MenuItem structure for the second item: */
  148. struct MenuItem my_second_item=
  149. {
  150.   &my_third_item,  /* NextItem, linked to the third item. */
  151.   0,               /* LeftEdge, 0 pixels out. */
  152.   10,              /* TopEdge, 10 lines down. */
  153.   150,             /* Width, 150 pixels wide. */
  154.   10,              /* Height, 10 lines high. */
  155.   ITEMTEXT|        /* Flags, render this item with text. */
  156.   ITEMENABLED|     /*        this item will be enabled. */
  157.   CHECKIT|         /*        it is an attribute item. */
  158.   HIGHCOMP,        /*        complement the colours when highlihted. */
  159.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  160.   (APTR) &my_second_text, /* ItemFill, pointer to the text. */
  161.   NULL,            /* SelectFill, nothing since we complement the col. */
  162.   0,               /* Command, no command-key sequence. */
  163.   NULL,            /* SubItem, no subitem list. */
  164.   MENUNULL,        /* NextSelect, no items selected. */
  165. };
  166.  
  167.  
  168.  
  169. /*************************************************************************/
  170. /*                          F I R S T   I T E M                          */
  171. /*************************************************************************/
  172.  
  173. /* The text for the first item: */
  174. struct IntuiText my_first_text=
  175. {
  176.   2,          /* FrontPen, black. */
  177.   0,          /* BackPen, not used since JAM1. */
  178.   JAM1,       /* DrawMode, do not change the background. */
  179.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  180.               /* This will leave enough space for the check mark. */
  181.   1,          /* TopEdge, 1 line down. */
  182.   NULL,       /* TextAttr, default font. */
  183.   "Plain",    /* IText, the string. */
  184.   NULL        /* NextItem, no link to other IntuiText structures. */
  185. };
  186.  
  187. /* The MenuItem structure for the first item: */
  188. struct MenuItem my_first_item=
  189. {
  190.   &my_second_item, /* NextItem, linked to the second item. */
  191.   0,               /* LeftEdge, 0 pixels out. */
  192.   0,               /* TopEdge, 0 lines down. */
  193.   150,             /* Width, 150 pixels wide. */
  194.   10,              /* Height, 10 lines high. */
  195.   ITEMTEXT|        /* Flags, render this item with text. */
  196.   ITEMENABLED|     /*        this item will be enabled. */
  197.   CHECKIT|         /*        it is an attribute item. */
  198.   CHECKED|         /*        this item is initially selected. */
  199.   HIGHCOMP,        /*        complement the colours when highlihted. */
  200.   0xFFFFFFFE,      /* MutualExclude, mutualexclude all items except the */
  201.                    /*                first one. */
  202.   (APTR) &my_first_text, /* ItemFill, pointer to the text. */
  203.   NULL,            /* SelectFill, nothing since we complement the col. */
  204.   0,               /* Command, no command-key sequence. */
  205.   NULL,            /* SubItem, no subitem list. */
  206.   MENUNULL,        /* NextSelect, no items selected. */
  207. };
  208.  
  209.  
  210.  
  211. /*************************************************************************/
  212. /*                              M E N U                                  */
  213. /*************************************************************************/
  214.  
  215. /* The Menu structure for the first (and only) menu: */
  216. struct Menu my_menu=
  217. {
  218.   NULL,          /* NextMenu, no more menu structures. */
  219.   0,             /* LeftEdge, left corner. */
  220.   0,             /* TopEdge, for the moment ignored by Intuition. */
  221.   50,            /* Width, 50 pixels wide. */
  222.   0,             /* Height, for the moment ignored by Intuition. */
  223.   MENUENABLED,   /* Flags, this menu will be enabled. */
  224.   "Mode",        /* MenuName, the string. */
  225.   &my_first_item /* FirstItem, pointer to the first item in the list. */
  226. };
  227.  
  228.  
  229.  
  230. /* Declare a pointer to a Window structure: */ 
  231. struct Window *my_window;
  232.  
  233. /* Declare and initialize your NewWindow structure: */
  234. struct NewWindow my_new_window=
  235. {
  236.   50,            /* LeftEdge    x position of the window. */
  237.   25,            /* TopEdge     y positio of the window. */
  238.   200,           /* Width       200 pixels wide. */
  239.   100,           /* Height      100 lines high. */
  240.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  241.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  242.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  243.                  /*             user has selected the Close window gad. */
  244.   MENUPICK,
  245.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  246.   WINDOWCLOSE|   /*             Close Gadget. */
  247.   WINDOWDRAG|    /*             Drag gadget. */
  248.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  249.   WINDOWSIZING|  /*             Sizing Gadget. */
  250.   ACTIVATE,      /*             The window should be Active when opened. */
  251.   NULL,          /* FirstGadget No Custom gadgets. */
  252.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  253.   "Style Editor",/* Title       Title of the window. */
  254.   NULL,          /* Screen      Connected to the Workbench Screen. */
  255.   NULL,          /* BitMap      No Custom BitMap. */
  256.   80,            /* MinWidth    We will not allow the window to become */
  257.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  258.   300,           /* MaxWidth    than 300 x 200. */
  259.   200,           /* MaxHeight */
  260.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  261. };
  262.  
  263.  
  264.  
  265. main()
  266. {
  267.   /* Boolean variable used for the while loop: */
  268.   BOOL close_me;
  269.  
  270.   /* Declare a variable in which we will store the IDCMP flag: */
  271.   ULONG class;
  272.   
  273.   /* If we recieve a MENUPICK event, the Code field of the message */
  274.   /* structure will contain the menu number of the first selected item. */
  275.   /* Declare a variable to store the Code value in, and an extra menu */
  276.   /* number variable: */
  277.   USHORT code, menu_number;
  278.   
  279.   /* Declare a MenuItem pointer: */
  280.   struct MenuItem *item;
  281.   
  282.   /* Declare a pointer to an IntuiMessage structure: */
  283.   struct IntuiMessage *my_message;
  284.  
  285.  
  286.  
  287.   /* Before we can use Intuition we need to open the Intuition Library: */
  288.   IntuitionBase = (struct IntuitionBase *)
  289.     OpenLibrary( "intuition.library", 0 );
  290.   
  291.   if( IntuitionBase == NULL )
  292.     exit(); /* Could NOT open the Intuition Library! */
  293.  
  294.  
  295.  
  296.   /* We will now try to open the window: */
  297.   my_window = (struct Window *) OpenWindow( &my_new_window );
  298.   
  299.   /* Have we opened the window succesfully? */
  300.   if(my_window == NULL)
  301.   {
  302.     /* Could NOT open the Window! */
  303.     
  304.     /* Close the Intuition Library since we have opened it: */
  305.     CloseLibrary( IntuitionBase );
  306.  
  307.     exit();  
  308.   }
  309.  
  310.  
  311.  
  312.   /* We have opened the window, and everything seems to be OK. */
  313.  
  314.  
  315.  
  316.   SetMenuStrip( my_window, &my_menu );
  317.   printf("Menustrip connected to window!\n");
  318.  
  319.  
  320.   close_me = FALSE;
  321.  
  322.   /* Stay in the while loop until the user has selected the Close window */
  323.   /* gadget: */
  324.   while( close_me == FALSE )
  325.   {
  326.     /* Wait until we have recieved a message: */
  327.     Wait( 1 << my_window->UserPort->mp_SigBit );
  328.  
  329.     /* As long as we collect messages sucessfully we stay in the loop: */
  330.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  331.     {
  332.       /* After we have collected the message we can read it, and save any */
  333.       /* important values which we maybe want to check later: */
  334.       class = my_message->Class;
  335.       code = my_message->Code;
  336.  
  337.  
  338.       /* After we have read it we reply as fast as possible: */
  339.       /* REMEMBER! Do never try to read a message after you have replied! */
  340.       /* Some other process has maybe changed it. */
  341.       ReplyMsg( my_message );
  342.  
  343.       /* Check which IDCMP flag was sent: */
  344.       if( class == CLOSEWINDOW )
  345.         close_me=TRUE; /* The user selected the Close window gadget! */  
  346.  
  347.       if(class == MENUPICK)
  348.       {
  349.         printf("\nMenu pick!\n");
  350.         menu_number = code;
  351.         
  352.         while( menu_number != MENUNULL )
  353.         {
  354.           /* Get the address of the item: */
  355.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  356.  
  357.  
  358.           /* Print out the menu number plus etc: */
  359.           printf("menu_number= %d\n", menu_number );
  360.           printf("MENUNUM = %d\n", MENUNUM(menu_number) );
  361.           printf("ITEMNUM = %d\n", ITEMNUM(menu_number) );
  362.           printf("SUBNUM  = %d\n", SUBNUM(menu_number) );
  363.  
  364.  
  365.           /* Get the following item's menu number: */
  366.           menu_number = item->NextSelect;
  367.         }
  368.       }
  369.     }
  370.   }
  371.  
  372.  
  373.  
  374.   printf("Menustrip removed from window!\n");
  375.   ClearMenuStrip( my_window );
  376.  
  377.  
  378.  
  379.   /* Close the window: */
  380.   CloseWindow( my_window );
  381.  
  382.  
  383.  
  384.   /* Close the Intuition Library since we have opened it: */
  385.   CloseLibrary( IntuitionBase );
  386.   
  387.   /* THE END */
  388. }
  389.